package com.SmartCare.service.impl.homeInfo;

import cn.hutool.core.util.ObjectUtil;
import com.SmartCare.common.enums.Constants;
import com.SmartCare.common.Result;
import com.SmartCare.common.dto.homeInfo.SuggestionDTO;
import com.SmartCare.mapper.homeInfo.CareHomeDetailsMapper;
import com.SmartCare.pojo.homeInfo.CareHomeDetails;
import com.SmartCare.service.homeInfo.SuggestionService;
import com.SmartCare.pojo.homeInfo.CareHomeSuggestion;
import com.SmartCare.mapper.homeInfo.CareHomeSuggestionMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 建议箱 服务实现类
 */
@Service
public class SuggestionServiceImpl extends ServiceImpl<CareHomeSuggestionMapper, CareHomeSuggestion> implements SuggestionService {
    @Resource
    private CareHomeDetailsMapper detailsMapper;
    /**
     * 添加意见
     */
    @Override
    public Result addNewSuggestion(SuggestionDTO suggestionDto) {
        if(suggestionDto.getContent().isEmpty()){
            return Result.error("请添加建议");
        }
        CareHomeDetails one1 = detailsMapper.selectOne(new QueryWrapper<CareHomeDetails>().eq("name", suggestionDto.getCareHomeName()));
        CareHomeDetails homeInfo = detailsMapper.selectOne(new LambdaQueryWrapper<CareHomeDetails>()
                .eq(CareHomeDetails::getId,one1.getId()));

        if(ObjectUtil.isNull(homeInfo)){
            return Result.error("养老院信息错误");
        }
        CareHomeSuggestion one = CareHomeSuggestion
                .builder()
                .careHomeId(homeInfo.getId())
                .userId(suggestionDto.getUserId())
                .content(suggestionDto.getContent())
                .pictureUrl(suggestionDto.getPictureUrl())
                .build();
        boolean res = save(one);
        if (!res){
            return Result.error("添加失败");
        }
        return Result.success(Constants.CODE_200,"添加成功");
    }
}
